﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReservoirModel;

namespace ReservoirDAL
{
    public class UserDAL
    {
        ReservoirConn Conn = new ReservoirConn();
        /// <summary>
        /// 列表显示信息
        /// </summary>
        /// <returns></returns>
        public List<TBRecord> GetList()
        {
            return Conn.TBRecord.ToList();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int Add(TBRecord info)
        {
            var res = new TBRecord()
            {
                UserId = info.UserId,
                District = info.District,
                Name = info.Name,
                Area = info.Area,
                Storage = info.Storage,
                Functional = info.Functional,
                Maximum = info.Maximum,
                WaterLevel = info.WaterLevel,
                Remarks = info.Remarks,
                CreationTime = DateTime.Now,
            };
            Conn.TBRecord.Add(res);
            return Conn.SaveChanges();
        }
        /// <summary>
        /// 通过当前用户UserId获取该用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<TBRecord> GetInfoById(string userID)
        {
            return Conn.TBRecord
               .Where(u => u.UserId == userID)
               .OrderByDescending(u => u.CreationTime)
               .ToList();
        }
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            TBRecord record = Conn.TBRecord.Find(id);
            Conn.TBRecord.Remove(record);
            int result = Conn.SaveChanges();
            return result;
        }
    }
}
